package cn.hyp.bantang.module.finance.service.parse;

import cn.hutool.core.util.StrUtil;
import cn.hyp.bantang.module.finance.pdf.Finance;
import cn.hyp.bantang.module.finance.pdf.PdfUtils;
import cn.hyp.bantang.module.infra.api.db.DbApi;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * pdf解析 Service 实现类
 *
 * @author 半唐
 */
@Service
@Validated
public class ParseServiceImpl implements  ParseService {
    @Resource
    DbApi dbApi;
    @Override
    public Finance parse(String pdfPath) {
        TableInfo tableInfoZcfz = dbApi.getTable(0l, "finance_base_zcfz");
        TableInfo tableInfoLr = dbApi.getTable(0l, "finance_base_lr");
        TableInfo tableInfoXj = dbApi.getTable(0l, "finance_base_xj");
        Map<String, String> mapZcfz = new LinkedHashMap<>(), mapLr = new LinkedHashMap<>(), mapXj = new LinkedHashMap<>();
        tableInfoZcfz.getFields().forEach(column->{
            String comment = column.getComment().replaceAll("\t","").replaceAll("\n","").replaceAll("单位：元", "");
            mapZcfz.put(comment, StrUtil.toCamelCase(column.getName()));
        });
        tableInfoLr.getFields().forEach(column->{
            String comment = column.getComment().replaceAll("\t","").replaceAll("\n","").replaceAll("单位：元", "");

            mapLr.put(comment,StrUtil.toCamelCase(column.getName()));
        });
        tableInfoXj.getFields().forEach(column->{
            String comment = column.getComment().replaceAll("\t","").replaceAll("\n","").replaceAll("单位：元", "");

            mapXj.put(comment,StrUtil.toCamelCase(column.getName()));
        });

        return PdfUtils.parseFinanceByReport(pdfPath, mapZcfz, mapLr, mapXj);
    }
}